﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class SelectLopSinhHoatDialog : Form
    {
        bool lockUpdate = false;
        public LopGoc SelectedLop
        {
            get
            {
                if (bsLop.Current == null)
                    return null;
                else
                    return bsLop.Current as LopGoc;
            }
        }
        public LopGoc lopCurrent;
        private bool m_IsChuyen = false;
        public SelectLopSinhHoatDialog(LopGoc lop, bool chuyen = false)
        {
            InitializeComponent();
            m_IsChuyen = chuyen;
            // Bind dữ liệu
            gridLop.AutoGenerateColumns = false;
            gridLop.DataSource = bsLop;
            colLop_TenLop.DataPropertyName = "TenLopGoc";
            lopCurrent = lop;
            LoadLoaiHinh();
        }

        private void OnBsLopCurrentChanged(object sender, EventArgs e)
        {
            CapNhatTrangThaiLopGoc();
        }

        private void CapNhatTrangThaiLopGoc()
        {
            btnOK.Enabled = (this.SelectedLop != null);
        }

        private void OnCbxNamTuyenSinhSelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadLopGoc();
            }

        }

        private void LoadLoaiHinh()
        {
            lockUpdate = true;
            var ds = BizLoaiHinhDaoTao.GetListLoaiHinh();
            //InitComboSuggestion(cmbLoaiHinh, ds, "TenLoaiHinh", "MaLoaiHinh", ds.Select(e => e.TenLoaiHinh).ToArray());
            cmbLoaiHinh.DataSource = ds;
            cmbLoaiHinh.DisplayMember = "TenLoaiHinh";
            cmbLoaiHinh.ValueMember = "MaLoaiHinh";
            if (lopCurrent != null)
            {
                cmbLoaiHinh.SelectedValue = lopCurrent.MaLoaiHinhDaoTao;
            }
            lockUpdate = false;
            LoadNamTuyenSinh();
        }

        private void LoadLopGoc()
        {
            lockUpdate = true;
            gridLop.DataSource = null;
            var ds = BizLopGoc.GetListLopGoc((cmbLoaiHinh.SelectedItem as LoaiHinhDaoTao).MaLoaiHinh, cbxNamTuyenSinh.SelectedItem as string);
            bsLop.DataSource = ds;
            gridLop.DataSource = bsLop;
            lockUpdate = false;
        }

        private void LoadNamTuyenSinh()
        {
            lockUpdate = true;
            cbxNamTuyenSinh.DataSource = null;
            var ds = BizLoaiHinhDaoTao.GetList_NamTuyenSinh((cmbLoaiHinh.SelectedItem as LoaiHinhDaoTao).MaLoaiHinh);
            //InitComboSuggestion(cmbNamTuyenSinh, ds, null, null, ds.ToArray());
            cbxNamTuyenSinh.DataSource = ds;
            lockUpdate = false;
            LoadLopGoc();
        }

        private void cmbLoaiHinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadNamTuyenSinh();
            }
        }
    }
}
